package com.charger.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charger.common.utils.DateUtils;
import com.charger.domain.*;
import com.charger.mapper.*;
import com.charger.utils.QDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.ParseException;
import java.util.*;

/**
 * @ClassName StatisticSchedu
 * @Description TODO
 * @Author QinKunPeng
 * @Date 2024年11月22日 20:32
 * @Version 1.0
 * 定时任务，记录统计所需数据
 */
@Component
public class StatisticSchedu {

    @Autowired
    ChargerOperationMiddleMapper chargerOperationMiddleMapper;

    @Autowired
    ChargerOperationMapper chargerOperationMapper;

    @Autowired
    ChargerFinancialMapper chargerFinancialMapper;

    @Autowired
    ChargerEnergyMapper chargerEnergyMapper;

    @Autowired
    QChargerMapper qChargerMapper;

    @Autowired
    QChargerStationMapper qChargerStationMapper;

    /**
     * @author QinKunPeng
     * @date 2024/11/24 19:49
     * 每日统计一次充电信息
     */
    @Scheduled(cron = "0 59 23 * * *")
    public void updateOperationByDay() {
        ChargerOperationMiddle chargerOperationMiddle = chargerOperationMiddleMapper.selectByDate(DateUtils.parseDateToStr("yyyy-MM-dd", new Date()));
        ChargerOperation chargerOperation = new ChargerOperation();
        if (chargerOperationMiddle != null) {
            chargerOperation.setCreateTime(new Date());
            chargerOperation.setChargeMoney(chargerOperationMiddle.getExpenses());
            chargerOperation.setChargeNum(chargerOperationMiddle.getChargeCount());
            chargerOperation.setCharges(chargerOperationMiddle.getChargePower());
            chargerOperation.setCustomerNum(chargerOperationMiddle.getClientCount());
            chargerOperationMapper.insert(chargerOperation);
        } else {
            chargerOperation.setCreateTime(new Date());
            chargerOperation.setCustomerNum(0l);
            chargerOperation.setCharges(0d);
            chargerOperation.setChargeMoney(0d);
            chargerOperation.setChargeNum(0l);
            chargerOperationMapper.insert(chargerOperation);
        }
    }

    /**
     * @author QinKunPeng
     * @date 2024/11/24 19:50
     * 每日统计一次账户流水
     */
    @Scheduled(cron = "0 30 23 * * *")
    public void updateFinancialOperationByDay() {
        List<ChargerFinancialMiddle> chargerFinancialMiddles = chargerFinancialMapper.listMiddleByDay(DateUtils.parseDateToStr("yyyy-MM-dd", new Date()));

        ChargerFinancial chargerFinancial = new ChargerFinancial();
        ChargerFinancial byDay = chargerFinancialMapper.getByDay();
        chargerFinancial.setCreateTime(new Date());
        chargerFinancial.setBalance(byDay.getBalance());
//            System.out.println(chargerFinancial);
        BigDecimal account = new BigDecimal(byDay.getBalance(), new MathContext(64));
//            System.out.println("余额" + account.doubleValue());
        if (chargerFinancialMiddles != null && chargerFinancialMiddles.size() > 0) {
            chargerFinancialMiddles.forEach(chargerFinancialMiddle -> {
                if (ChargeType.REFUND.value().equals(chargerFinancialMiddle.getTransactionType())) {
                    BigDecimal bigDecimal = new BigDecimal(chargerFinancialMiddle.getExpenses());
                    BigDecimal add = new BigDecimal(chargerFinancial.getRefund());
                    double v = add.add(bigDecimal).doubleValue();
                    chargerFinancial.setRefund(v);
                    BigDecimal sub = new BigDecimal(chargerFinancial.getBalance());
                    chargerFinancial.setBalance(sub.subtract(bigDecimal).doubleValue());
                    BigDecimal dayAllPay = new BigDecimal(chargerFinancial.getAllPay());
                    chargerFinancial.setAllPay(dayAllPay.subtract(bigDecimal).doubleValue());
                } else if (ChargeType.PRE_PAY.value().equals(chargerFinancialMiddle.getTransactionType())) {
                    BigDecimal bigDecimal = new BigDecimal(chargerFinancialMiddle.getExpenses());
                    BigDecimal add = new BigDecimal(chargerFinancial.getPrePay());
                    double v = add.add(bigDecimal).doubleValue();
                    chargerFinancial.setPrePay(v);
//                    BigDecimal addBalance = new BigDecimal(chargerFinancial.getBalance());
//                    chargerFinancial.setBalance(addBalance.subtract(bigDecimal).doubleValue());
                } else if (ChargeType.CHARGE_PAY.value().equals(chargerFinancialMiddle.getTransactionType())) {
                    if (ChargeType.WECHART_PAY.value().equals(chargerFinancialMiddle.getPaymentMethod())) {
                        BigDecimal bigDecimal = new BigDecimal(chargerFinancialMiddle.getExpenses());
                        BigDecimal add = new BigDecimal(chargerFinancial.getWechatPay());
                        double v = add.add(bigDecimal).doubleValue();
                        chargerFinancial.setWechatPay(v);
                        BigDecimal addBalance = new BigDecimal(chargerFinancial.getBalance());
                        chargerFinancial.setBalance(addBalance.add(bigDecimal).doubleValue());
                        BigDecimal dayAllPay = new BigDecimal(chargerFinancial.getAllPay());
                        chargerFinancial.setAllPay(dayAllPay.add(bigDecimal).doubleValue());
                    } else if (ChargeType.ALI_PAY.value().equals(chargerFinancialMiddle.getPaymentMethod())) {
                        BigDecimal bigDecimal = new BigDecimal(chargerFinancialMiddle.getExpenses());
                        BigDecimal add = new BigDecimal(chargerFinancial.getAliPay());
                        double v = add.add(bigDecimal).doubleValue();
                        chargerFinancial.setAliPay(v);
                        BigDecimal addBalance = new BigDecimal(chargerFinancial.getBalance());
                        chargerFinancial.setBalance(addBalance.add(bigDecimal).doubleValue());
                        BigDecimal dayAllPay = new BigDecimal(chargerFinancial.getAllPay());
                        chargerFinancial.setAllPay(dayAllPay.add(bigDecimal).doubleValue());
                    } else if (ChargeType.CARD_PAY.value().equals(chargerFinancialMiddle.getPaymentMethod())) {
                        BigDecimal bigDecimal = new BigDecimal(chargerFinancialMiddle.getExpenses());
                        BigDecimal add = new BigDecimal(chargerFinancial.getCardPay());
                        double v = add.add(bigDecimal).doubleValue();
                        chargerFinancial.setCardPay(v);
                        BigDecimal addBalance = new BigDecimal(chargerFinancial.getBalance());
                        chargerFinancial.setBalance(addBalance.add(bigDecimal).doubleValue());
                        BigDecimal dayAllPay = new BigDecimal(chargerFinancial.getAllPay());
                        chargerFinancial.setAllPay(dayAllPay.add(bigDecimal).doubleValue());
                    }
                }
            });

        }
        chargerFinancialMapper.insertChargerFinancial(chargerFinancial);
    }

    @Scheduled(cron = "0 0 * * * *")
    public void energyStatistic() {
        Date now = new Date();
        String day = DateUtils.parseDateToStr("yyyy-MM-dd", now);
        String startTime = QDateUtils.beforeTime(60, now);
        String endTime = DateUtils.parseDateToStr("HH:mm:ss", now);
        System.out.println(day + "   " + startTime + "   " + endTime);
        List<ChargerEnergy> chargerEnergies = chargerEnergyMapper.selectByDayAndTime(day, startTime, endTime);
        chargerEnergies.forEach(chargerEnergy -> {
            chargerEnergy.setCreateTime(new Date());
            chargerEnergyMapper.insertChargerEnergy(chargerEnergy);
        });
    }

    //    @Scheduled(cron = "0/5 * * * * *")
    public void testEnergy() throws ParseException {
        Map params = new HashMap();
        params.put("day", DateUtils.parseDateToStr("yyyy-MM-dd", new Date(new Date().getTime() - 1000 * 60 * 60 * 24)));
        List<ChargerEnergyMiddle> chargerEnergyMiddles = chargerEnergyMapper.selectByTime(params);
        System.out.println(Arrays.toString(chargerEnergyMiddles.toArray()));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.parseDate(chargerEnergyMiddles.get(0).getDay(), "yyyy-MM-dd"));
        List<String> times = QDateUtils.getTime();
        ChargerEnergyRes chargerEnergyRes = new ChargerEnergyRes();
        out:
        for (int i = 0; i < times.size(); i++) {
            for (int j = 0; j < chargerEnergyMiddles.size(); j++) {
                if (times.get(i).equals(chargerEnergyMiddles.get(j).getTime())) {
                    chargerEnergyRes.getEnergys().add(chargerEnergyMiddles.get(j).getEnergy());
                    continue out;
                }
            }
            chargerEnergyRes.getEnergys().add(0d);
        }
        chargerEnergyRes.setWeek(QDateUtils.weeks()[calendar.get(Calendar.DAY_OF_WEEK)]);
        System.out.println(chargerEnergyRes);
    }

    //    @Scheduled(cron = "0/5 * * * * *")
    public void testCharger() {
        QChargerStation qChargerStation = qChargerStationMapper.selectById(1);
        if (qChargerStation != null) {
            QueryWrapper<QCharger> q = new QueryWrapper<>();
            q.eq("chargingStation_id", qChargerStation.getId());
            List<QCharger> qChargers = qChargerMapper.selectList(q);
            qChargerStation.setChargers(qChargers);
            System.out.println(qChargerStation);
        }
    }

}
